package com.chen.baozhangmonitor;

import android.graphics.Bitmap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.ColorTemplate;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private TextView mZhubo;
    private TextView mColumn;
    private TextView mBaozhang;

    private RecyclerView recyclerView;
    private RecycleAdapter recycleAdapter;

    private PieChart pieChart;
    private List<String> title;
    private List<Integer> amount;

    private List<String> name;
    private List<String> shangbang;
    private List<String> baozhang;
    private List<Bitmap> headImg;

    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initData();
    }



    private void init() {
        title = new ArrayList<>();
        amount = new ArrayList<>();
        requestQueue = Volley.newRequestQueue(getApplicationContext());
        mZhubo = findViewById(R.id.zhubo_amount);
        mColumn = findViewById(R.id.column_amount);
        mBaozhang = findViewById(R.id.baozhang);
        pieChart = findViewById(R.id.pie_chart);

        recyclerView = findViewById(R.id.rv_item);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(layoutManager);
        recycleAdapter = new RecycleAdapter();
        recyclerView.setAdapter(recycleAdapter);

        pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                pieChart.setCenterText(title.get((int) h.getX())+"\n暴涨次数"+amount.get((int) h.getX()));
            }

            @Override
            public void onNothingSelected() {
                pieChart.setCenterText("");
            }
        });
    }

    private void initData() {
        StringRequest request = new StringRequest(Request.Method.POST, "https://www.zhiboshuju.com/bz/getBZInfo", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject request = new JSONObject(s);
                    JSONObject data = new JSONObject(request.getString("data"));
                    mZhubo.setText(data.getString("zhuboAmount"));
                    mColumn.setText(data.getString("columnAmount"));
                    mBaozhang.setText((int)(data.getDouble("baozhang")+0.5)+"");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(),volleyError.toString(),Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("openId","ofYto00X_EzHKjEHKuwKNTFhSIfY");
                return map;
            }
        };
        StringRequest pieChart = new StringRequest(Request.Method.POST, "https://www.zhiboshuju.com/bz/getClassNumInfo", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject request = new JSONObject(s);
                    JSONArray data = request.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject datai = data.getJSONObject(i);
                        title.add(datai.getString("title"));
                        amount.add(Integer.parseInt(datai.getString("amount")));
                    }
                    showPieChart();
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i("error", e.toString());
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(getApplicationContext(),volleyError.toString(),Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("openId","ofYto00X_EzHKjEHKuwKNTFhSIfY");
                return map;
            }
        };
        StringRequest lineChart = new StringRequest(Request.Method.POST, "https://www.zhiboshuju.com/bz/getChannelInfo", new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                try {
                    JSONObject request = new JSONObject(s);
                    JSONArray data = request.getJSONArray("data");
                    name = new ArrayList<>();
                    shangbang = new ArrayList<>();
                    baozhang = new ArrayList<>();
                    headImg = new ArrayList<>();
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject item = data.getJSONObject(i);
                        String accountName = item.getString("accountName");
                        if (i==0||!accountName.equals(name.get(name.size()-1))){
                            name.add(accountName);
                            String amount = item.getString("amount");
                            shangbang.add(amount);
                            String baozhangAmount = item.getString("baozhang");
                            baozhang.add(baozhangAmount);
//                            final String head = item.getString("headImg");
//                            head.replace("\\/","/");
//                            String url;
//                            if (!head.contains("https:")&&!head.contains("http:")){
//                                url = "https:"+head;
//                            }else{url = head;}
//                            Log.i("aaa", url);
//                            ImageRequest imageRequest = new ImageRequest(url, new Response.Listener<Bitmap>() {
//                                @Override
//                                public void onResponse(Bitmap bitmap) {
//                                    headImg.add(bitmap);
//                                }
//                            }, 0, 0, Bitmap.Config.ARGB_4444, new Response.ErrorListener() {
//                                @Override
//                                public void onErrorResponse(VolleyError volleyError) {
//
//                                }
//                            });
//                            requestQueue.add(imageRequest);requestQueue.add(imageRequest);
                        }

                    }
                    recycleAdapter.setData(name,shangbang,baozhang);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String,String> map = new HashMap<>();
                map.put("dateTime","20190611");
                map.put("openId","ofYto00X_EzHKjEHKuwKNTFhSIfY");
                return map;
            }
        };
        requestQueue.add(request);
        requestQueue.add(pieChart);
        requestQueue.add(lineChart);
    }

    private void showPieChart() {
        // 设置每份所占数量
        List<PieEntry> yvals = new ArrayList<>();
        for (int i = 0; i < title.size(); i++) {
            yvals.add(new PieEntry(amount.get(i), title.get(i)));
        }


        PieChartManager pieChartManager = new PieChartManager(pieChart);
        pieChartManager.showRingPieChart(yvals);
    }


}
